"use strict";

/*
 * MikoPBX - free phone system for small business
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 */

/** global: DebuggerInfo, sessionStorage, PbxApi */

/**
 * Object representing the provider status loop worker.
 *
 * @module providersStatusLoopWorker
 */
var providersStatusLoopWorker = {
  /**
   * Time in milliseconds before fetching new status request.
   * @type {number}
   */
  timeOut: 3000,

  /**
   * The id of the timer function for the status worker.
   * @type {number}
   */
  timeOutHandle: 0,

  /**
   * Object to store provider statuses.
   * @type {Object}
   */
  providerStatuses: {},

  /**
   * Initializes the provider status loop worker.
   */
  initialize: function initialize() {
    DebuggerInfo.initialize();
    var previousStatuses = sessionStorage.getItem('ProviderStatuses');

    if (previousStatuses !== null) {
      providersStatusLoopWorker.providerStatuses = JSON.parse(previousStatuses);
    }

    providersStatusLoopWorker.restartWorker();
  },

  /**
   * Restarts the status worker.
   */
  restartWorker: function restartWorker() {
    window.clearTimeout(providersStatusLoopWorker.timeoutHandle);
    providersStatusLoopWorker.worker();
  },

  /**
   * Executes the status worker.
   */
  worker: function worker() {
    window.clearTimeout(providersStatusLoopWorker.timeoutHandle);
    PbxApi.GetSipProvidersStatuses(providersStatusLoopWorker.cbRefreshProvidersStatus);
    PbxApi.GetIaxProvidersStatuses(providersStatusLoopWorker.cbRefreshProvidersStatus);
  },

  /**
   * Callback function to accumulate provider statuses.
   * @param {Array} response - Response containing provider statuses.
   */
  cbRefreshProvidersStatus: function cbRefreshProvidersStatus(response) {
    providersStatusLoopWorker.timeoutHandle = window.setTimeout(providersStatusLoopWorker.worker, providersStatusLoopWorker.timeOut);
    if (response.length === 0 || response === false) return;
    $.each(response, function (key, value) {
      if (value.state !== undefined) {
        providersStatusLoopWorker.providerStatuses[value.id] = value.state.toUpperCase();
      }
    });
    sessionStorage.setItem('ProviderStatuses', JSON.stringify(providersStatusLoopWorker.providerStatuses));
    providersStatusLoopWorker.refreshVisualisation();
  },

  /**
   * Refreshes the visualization of provider statuses.
   */
  refreshVisualisation: function refreshVisualisation() {
    // Iterate over the response data and create HTML table rows for each provider status
    // to shows it on debug slider by double press esc button
    var htmlTable = '<table class="ui very compact table">';
    $.each(providersStatusLoopWorker.providerStatuses, function (key, value) {
      htmlTable += '<tr>';
      htmlTable += "<td>".concat(key, "</td>");
      htmlTable += "<td>".concat(value, "</td>");
      htmlTable += '</tr>';
    });
    htmlTable += '</table>';
    DebuggerInfo.UpdateContent(htmlTable); // Define label styles for different statuses

    var green = '<div class="ui green empty circular label" style="width: 1px;height: 1px;"></div>';
    var grey = '<div class="ui grey empty circular label" style="width: 1px;height: 1px;"></div>';
    var yellow = '<div class="ui yellow empty circular label" style="width: 1px;height: 1px;"></div>'; // Update provider status and failure information in the UI

    $('tr.provider-row').each(function (index, obj) {
      var uniqid = $(obj).attr('id');

      if (providersStatusLoopWorker.providerStatuses[uniqid] !== undefined) {
        switch (providersStatusLoopWorker.providerStatuses[uniqid]) {
          case 'REGISTERED':
            $(obj).find('.provider-status').html(green);
            $(obj).find('.failure').text('');
            break;

          case 'OK':
            $(obj).find('.provider-status').html(yellow);
            $(obj).find('.failure').text('');
            break;

          case 'OFF':
            $(obj).find('.provider-status').html(grey);
            $(obj).find('.failure').text('');
            break;

          default:
            $(obj).find('.provider-status').html(grey);
            $(obj).find('.failure').text(providersStatusLoopWorker.providerStatuses[uniqid]);
            break;
        }
      } else {
        $(obj).find('.provider-status').html(grey);
      }
    });
  }
};
/**
 *  Initialize providers status worker on document ready
 */

$(document).ready(function () {
  providersStatusLoopWorker.initialize();
});
//# sourceMappingURL=data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6IDFweDtcIj48L2Rpdj4nO1xuICAgICAgICBjb25zdCBncmV5ID0gJzxkaXYgY2xhc3M9XCJ1aSBncmV5IGVtcHR5IGNpcmN1bGFyIGxhYmVsXCIgc3R5bGU9XCJ3aWR0aDogMXB4O2hlaWdodDogMXB4O1wiPjwvZGl2Pic7XG4gICAgICAgIGNvbnN0IHllbGxvdyA9ICc8ZGl2IGNsYXNzPVwidWkgeWVsbG93IGVtcHR5IGNpcmN1bGFyIGxhYmVsXCIgc3R5bGU9XCJ3aWR0aDogMXB4O2hlaWdodDogMXB4O1wiPjwvZGl2Pic7XG5cbiAgICAgICAgLy8gVXBkYXRlIHByb3ZpZGVyIHN0YXR1cyBhbmQgZmFpbHVyZSBpbmZvcm1hdGlvbiBpbiB0aGUgVUlcbiAgICAgICAgJCgndHIucHJvdmlkZXItcm93JykuZWFjaCgoaW5kZXgsIG9iaikgPT4ge1xuICAgICAgICAgICAgY29uc3QgdW5pcWlkID0gJChvYmopLmF0dHIoJ2lkJyk7XG4gICAgICAgICAgICBpZiAocHJvdmlkZXJzU3RhdHVzTG9vcFdvcmtlci5wcm92aWRlclN0YXR1c2VzW3VuaXFpZF0gIT09IHVuZGVmaW5lZCkge1xuICAgICAgICAgICAgICAgIHN3aXRjaCAocHJvdmlkZXJzU3RhdHVzTG9vcFdvcmtlci5wcm92aWRlclN0YXR1c2VzW3VuaXFpZF0pIHtcbiAgICAgICAgICAgICAgICAgICAgY2FzZSAnUkVHSVNURVJFRCc6XG4gICAgICAgICAgICAgICAgICAgICAgICAkKG9iaikuZmluZCgnLnByb3ZpZGVyLXN0YXR1cycpLmh0bWwoZ3JlZW4pO1xuICAgICAgICAgICAgICAgICAgICAgICAgJChvYmopLmZpbmQoJy5mYWlsdXJlJykudGV4dCgnJyk7XG4gICAgICAgICAgICAgICAgICAgICAgICBicmVhaztcbiAgICAgICAgICAgICAgICAgICAgY2FzZSAnT0snOlxuICAgICAgICAgICAgICAgICAgICAgICAgJChvYmopLmZpbmQoJy5wcm92aWRlci1zdGF0dXMnKS5odG1sKHllbGxvdyk7XG4gICAgICAgICAgICAgICAgICAgICAgICAkKG9iaikuZmluZCgnLmZhaWx1cmUnKS50ZXh0KCcnKTtcbiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrO1xuICAgICAgICAgICAgICAgICAgICBjYXNlICdPRkYnOlxuICAgICAgICAgICAgICAgICAgICAgICAgJChvYmopLmZpbmQoJy5wcm92aWRlci1zdGF0dXMnKS5odG1sKGdyZXkpO1xuICAgICAgICAgICAgICAgICAgICAgICAgJChvYmopLmZpbmQoJy5mYWlsdXJlJykudGV4dCgnJyk7XG4gICAgICAgICAgICAgICAgICAgICAgICBicmVhaztcbiAgICAgICAgICAgICAgICAgICAgZGVmYXVsdDpcbiAgICAgICAgICAgICAgICAgICAgICAgICQob2JqKS5maW5kKCcucHJvdmlkZXItc3RhdHVzJykuaHRtbChncmV5KTtcbiAgICAgICAgICAgICAgICAgICAgICAgICQob2JqKS5maW5kKCcuZmFpbHVyZScpLnRleHQocHJvdmlkZXJzU3RhdHVzTG9vcFdvcmtlci5wcm92aWRlclN0YXR1c2VzW3VuaXFpZF0pO1xuICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7XG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfSBlbHNlIHtcbiAgICAgICAgICAgICAgICAkKG9iaikuZmluZCgnLnByb3ZpZGVyLXN0YXR1cycpLmh0bWwoZ3JleSk7XG4gICAgICAgICAgICB9XG4gICAgICAgIH0pO1xuICAgIH0sXG59O1xuXG4vKipcbiAqICBJbml0aWFsaXplIHByb3ZpZGVycyBzdGF0dXMgd29ya2VyIG9uIGRvY3VtZW50IHJlYWR5XG4gKi9cbiQoZG9jdW1lbnQpLnJlYWR5KCgpID0+IHtcbiAgICBwcm92aWRlcnNTdGF0dXNMb29wV29ya2VyLmluaXRpYWxpemUoKTtcbn0pOyJdfQ==